package com.xxk.rocket.star.core.utils;

import com.xxk.rocket.star.core.exception.StargateException;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"unchecked"})
public class EncodeAndDecodeFactory {
  private static final Map<Class, Object> encodeAndDecode = new ConcurrentHashMap<Class, Object>();

  public static Object getEncodeOrDecode(Class aClass) {
    Object o = encodeAndDecode.get(aClass);
    if (o == null) {
      synchronized (encodeAndDecode) {
        try {
          o = encodeAndDecode.get(aClass);
          if (o == null) {
            o = aClass.getDeclaredConstructor().newInstance();
            encodeAndDecode.put(aClass, o);
          }
        } catch (Exception e) {
          throw new StargateException(e);
        }
      }
    }
    return o;
  }
}
